package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyXiWuJinFu;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  栖梧金服
 *
 *  @author WJB
 */
@ApiPlan(productId = "45", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5XiWuJinFu implements ApiOrgPhoneMd5ConvertHandler {

    private static final String SOURCE = "信息流-广奥通";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobileMd5", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("source", SOURCE);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("status"), 200);

        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyXiWuJinFu data = new OrgApplyXiWuJinFu();
        data.setName(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(0);
        data.setGender(sex);

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  不能包含市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            data.setCity(city);
        }
        data.setApplyTime(ClassConvertUtils.toStr(System.currentTimeMillis()));
        //  贷款额度
        data.setLoanMoney(apiFieldConverter.getLoanAmountUnit());
        data.setSource(SOURCE);
        data.setOtherInfo(apiFieldConverter.getExtendInfo());
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("status"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
